1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file=
"WorkerInGame.cs" company="Exit Games GmbH">
3 // Part of: Photon Unity Networking
4 // </copyright>
5 // --------------------------------------------------------------------------------------------------------------------

6
7 using
UnityEngine;
8
9 public
class WorkerInGame : Photon.MonoBehaviour
10 {
11     
public Transform playerPrefab;
12
13     
public void Awake()
14     {
15         
// in case we started this demo with the wrong scene being active, simply load the menu scene
16         
if (!PhotonNetwork.connected)
17         {
18             Application.LoadLevel(WorkerMenu.SceneNameMenu);
19             
return;
20         }
21
22         
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
23         PhotonNetwork.Instantiate(
this.playerPrefab.name, transform.position, Quaternion.identity, 0);
24     }
25
26     
public void OnGUI()
27     {
28         
if (GUILayout.Button("Return to Lobby"))
29         {
30             PhotonNetwork.LeaveRoom();
// we will load the menu level when we successfully left the room
31         }
32     }
33
34     
public void OnMasterClientSwitched(PhotonPlayer player)
35     {
36         Debug.Log(
"OnMasterClientSwitched: " + player);
37
38         
string message;
39         InRoomChat chatComponent = GetComponent<InRoomChat>();
// if we find a InRoomChat component, we print out a short message
40
41         
if (chatComponent != null)
42         {
43             
// to check if this client is the new master...
44             
if (player.isLocal)
45             {
46                 message =
"You are Master Client now.";
47             }
48             
else
49             {
50                 message = player.name +
" is Master Client now.";
51             }
52
53
54             chatComponent.AddLine(message);
// the Chat method is a RPC. as we don't want to send an RPC and neither create a PhotonMessageInfo, lets call AddLine()
55         }
56     }
57
58     
public void OnLeftRoom()
59     {
60         Debug.Log(
"OnLeftRoom (local)");
61         
62         
// back to main menu
63         Application.LoadLevel(WorkerMenu.SceneNameMenu);
64     }
65
66     
public void OnDisconnectedFromPhoton()
67     {
68         Debug.Log(
"OnDisconnectedFromPhoton");
69
70         
// back to main menu
71         Application.LoadLevel(WorkerMenu.SceneNameMenu);
72     }
73
74     
public void OnPhotonInstantiate(PhotonMessageInfo info)
75     {
76         Debug.Log(
"OnPhotonInstantiate " + info.sender); // you could use this info to store this or react
77     }
78
79     
public void OnPhotonPlayerConnected(PhotonPlayer player)
80     {
81         Debug.Log(
"OnPhotonPlayerConnected: " + player);
82     }
83
84     
public void OnPhotonPlayerDisconnected(PhotonPlayer player)
85     {
86         Debug.Log(
"OnPlayerDisconneced: " + player);
87     }
88
89     
public void OnFailedToConnectToPhoton()
90     {
91         Debug.Log(
"OnFailedToConnectToPhoton");
92
93         
// back to main menu
94         Application.LoadLevel(WorkerMenu.SceneNameMenu);
95     }
96 }


--------------------------------------------------------------------------------------------------------------------

Part of: Photon Unity Networking

--------------------------------------------------------------------------------------------------------------------

in case we started this demo with the wrong scene being active, simply load the menu scene

we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate

PhotonNetwork.LeaveRoom(); we will load the menu level when we successfully left the room

InRoomChat chatComponent = GetComponent(); if we find a InRoomChat component, we print out a short message

to check if this client is the new master...

chatComponent.AddLine(message); the Chat method is a RPC. as we don't want to send an RPC and neither create a PhotonMessageInfo, lets call AddLine()

back to main menu

back to main menu

Debug.Log("OnPhotonInstantiate " + info.sender); you could use this info to store this or react

back to main menu




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.435 lượt xem

Gõ tìm kiếm nhanh...